package cn.changein.framework.admin.setting.area.controller;

import cn.changein.framework.base.auth.annotation.Permission;
import cn.changein.framework.base.controller.BaseController;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.core.constant.Const;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.setting.area.entity.Area;
import cn.changein.framework.model.setting.area.model.params.AreaParam;
import cn.changein.framework.model.setting.area.model.result.AreaChain;
import cn.changein.framework.model.setting.area.model.result.AreaResult;
import cn.changein.framework.model.setting.area.model.result.ListTreeResult;
import cn.changein.framework.service.setting.area.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 地区表控制器
 *

 * @Date 2020-12-15 16:33:48
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {

    private String PREFIX = "/modular/setting/area";

    @Autowired
    private AreaService areaService;

    /**
     * 跳转到主页面
     *

     * @Date 2020-12-15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/area.html";
    }

    /**
     * 新增页面
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/area_add.html";
    }

    /**
     * 编辑页面
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/area_edit.html";
    }

    /**
     * 新增接口
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AreaParam areaParam) {
        this.areaService.add(areaParam);
        return ResponseData.success(areaParam);
    }

    /**
     * 编辑接口
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AreaParam areaParam) {
        this.areaService.update(areaParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AreaParam areaParam) {
        this.areaService.delete(areaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *

     * @Date 2020-12-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AreaParam areaParam) {
        Area detail = this.areaService.getById(areaParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *

     * @Date 2020-12-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public PageInfo list(AreaParam areaParam) {
        return this.areaService.adminFindPageBySpec(areaParam);
    }

    /**
     * @Description: 获取所有地区数据
     * @Date:2020/12/15 21:03
     * @Param: [areaParam]

     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping(value = "/listTree")
    @ResponseBody
    public Object listTree(AreaParam areaParam) {
        List<AreaResult> list = this.areaService.findListAllByAreaParam(areaParam);
        PageInfo result = new PageInfo();
        result.setData(list);
        return result;
    }

    /**
     * @Description: 树形区域跳转
     * @Date:2020/12/16 17:55
     * @Param: [areaParam]

     */
    @RequestMapping(value = "/listTreeJump")
    @ResponseBody
    public Object listTreeJump(AreaParam areaParam) {
        List<ListTreeResult> list = this.areaService.findListTreeInfo(areaParam);
        PageInfo result = new PageInfo();
        result.setData(list);
        return result;
    }

    /**
     * 根据市级id返查找区县列表和省份Id
     */
    @RequestMapping(value = "/findAreaChain")
    @ResponseBody
    public ResponseData findAreaChain(AreaParam areaParam) {
        AreaChain areaChain = areaService.findAreaChain(areaParam);
        return ResponseData.success(areaChain);
    }


}


